home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / e_SML / excel.sml < prev    next >
Encoding:
Text File  |  1997-07-04  |  5.5 KB  |  193 lines  |  [TEXT/Moml]

  1. (* excel.sml      *)
  2. (* 17 May 1997  e *)
  3.  
  4. (* using AppleScript to work with Excel spreadsheets *)
  5.  
  6. (*
  7. load "Int";
  8. load "Real";
  9. load "AppleScript";
  10. open AppleScript;
  11. *)
  12.  
  13.  
  14. fun tell_excel_str s =
  15.  "tell application \"Microsoft Excel\"\n" ^ s ^ "\nend tell";
  16.  
  17. fun run_excel s = as_run_text( tell_excel_str s );
  18.  
  19. fun rYcX y x =
  20.   "\"R" ^ (Int.toString y) ^ "C" ^ (Int.toString x) ^ "\""
  21. ;
  22.  
  23. fun toStrStr v = "\"" ^ v ^ "\"" ;
  24.  
  25. fun toIntStr v = "\"" ^ (Int.toString v) ^ "\"" ;
  26.  
  27. fun toRealStr v = "\"" ^ (Real.toString v) ^ "\"" ;
  28.  
  29. fun get_cell y x = (* read RyCx *)
  30.   run_excel( "get value of Cell " ^ (rYcX y x) ^ " of Document 1" )
  31. ;
  32.  
  33. fun get_cell_as y x ty = (* read RyCx as type ty *)
  34.   run_excel( "get value of Cell " ^ (rYcX y x) ^ " of Document 1 as " ^ ty )
  35. ;
  36.  
  37. fun get_cell_int y x = get_cell_as y x "integer";
  38. fun get_cell_real y x = get_cell_as y x "real";
  39. fun get_cell_string y x = get_cell_as y x "string";
  40.  
  41. fun set_cell y x v = (* write v into RyCx *)
  42.   run_excel( "set value of Cell " ^ (rYcX y x) ^ " of Document 1 to " ^ v )
  43. ;
  44.  
  45. fun set_cell_int y x v = (* write the integter v into RyCx *)
  46.   set_cell y x (toIntStr v)
  47. ;
  48.  
  49. fun set_cell_real y x v = (* write the real v into RyCx *)
  50.   set_cell y x (toRealStr v)
  51. ;
  52.  
  53. fun set_cell_string y x v = (* write the string v into RyCx *)
  54.   set_cell y x (toStrStr v)
  55. ;
  56.  
  57.  
  58. (* examples
  59. fun excel_fill rstart rend cstart cend v =
  60.   let fun dorow r = if r > rend then ()
  61.                     else
  62.     let fun docol c = if c > cend then () 
  63.                       else ( set_cell r c v; docol (c + 1) )
  64.     in docol cstart;
  65.        dorow (r + 1)
  66.     end
  67.   in dorow rstart
  68.   end;
  69. excel_fill 5 7 5 7 (toStrStr "e");
  70. excel_fill 6 9 1 5 (toRealStr 3.141592685);
  71.  
  72. run_excel "the name of Document 1";
  73. run_excel "the name of Document 2"
  74.  handle AppleScriptErr (n,s) => (Int.toString n ^ "\n" ^ s);
  75.  
  76. run_excel "get (count document 1 each column) as integer";
  77. run_excel "get (count document 1 each row) as integer";
  78. run_excel "delete Column 3";
  79.  
  80. val ctscrpt = as_compile "get (count document 1 each column) as integer";
  81. as_run_script ctscrpt
  82.  handle AppleScriptErr (n,s) => s;
  83.  
  84. as_dispose ctscrpt
  85.  handle AppleScriptErr (n,s) => print s;
  86.  
  87. as_dispose ctscrpt
  88.  handle AppleScriptErr (n,s) => print s;
  89.  
  90. val ctscrpt =
  91.  as_compile (tell_excel_str "get (count document 1 each column) as integer");
  92. as_run_script ctscrpt
  93.  handle AppleScriptErr (n,s) => (Int.toString n ^ s);
  94.  
  95. *)
  96.  
  97. (* one liners, thanks to J.P. Adams...
  98.  
  99.    get Range "R1C1:R3C1" of Document 1 as text
  100.    get Range "TOM" of Document 1 as text
  101.    get value of Cell 3 of Row 2 of Document 1
  102.    get value of Cell "Fred" of Document 1
  103.    get value of Cell "R1C1" of Document 1
  104.    set Range "R1C1:R3C1" of Document 1 to {"4", "5", "6"}
  105.    set Range "Tom" of Document 1 to {"4", "5", "6"}
  106.    set value of Cell 3 of Row 2 of Document 1 to "99"
  107.    set value of Cell "Fred" of Document 1 to "99"
  108.    set value of Cell "R1C1" of Document 1 to "99"
  109.  
  110.    make new Window
  111.    exists Window "AE Test"
  112.    exists table "Table_1"
  113.    exists Range "Tom"
  114.    exists Cell "fred"
  115.    exists Document "AE Test"
  116.    delete Cell "Fred" -- No reply, check spreadsheet for deletion
  117.    delete table "Table_1"
  118.    duplicate Cell "Bob" to Cell "Fred"
  119.    duplicate Cell "R1C1" to Cell "R6C1"
  120.    move Cell "R1C1" to Cell "R6C1"
  121.    move Cell "Bob" to Cell "R1C1"
  122.    set the font of Cell "R1C1" to "Times"
  123.  
  124.    set the size of Cell "R1C1" to "36" -- increases height of entire row
  125.    set the name of Cell "R1C1" to "Texas_Cities"
  126.    set the formula of Cell "R1C1" to "=5*5" --cell should show 25
  127.    set the bold of Cell "R1C1" to true
  128.    set the italic of Cell "R1C1" to true
  129.    set the underline of Cell "R1C1" to true
  130.    set the strikethrough of Cell "R1C1" to true
  131.    set the outline of Cell "R1C1" to true
  132.    set the shadow of Cell "R1C1" to true
  133.    set the plain of Cell "R1C1" to true
  134. --see Number dialog, Format menu
  135.    set the number format of Cell "R1C1" to "0.00%"  
  136. --You must use the index (1-7) for the alignment found in the
  137. -- Excel Function Reference page 15-16
  138.    set the alignment of Cell "R1C1" to "3"
  139. --the borders are set using a macro code
  140. -- Excel Function Reference page 39
  141.    set the left border of Cell "R1C1" to "5"
  142.  
  143.    set the font of Range "Table_1" to "times"
  144.    set the height of Row 6 to "24"
  145.    set the font of Row 7 to "times"
  146.    set the font of Column 1 to "times"
  147.    set the width of Column 2 to "14"
  148.    set the name of Column 2 to "STATES"
  149.  
  150.    get name of Window 1
  151.    the type of Window 1
  152.    the selection of Window 1 as string
  153.    the user selection of Window 1 as string
  154.    the bounds of Window 1
  155.    the titled of Window 1
  156.    the index of Window "AE Test"
  157.    the floating of Window 1
  158.    the modal of Window 1
  159.    the resizable of Window 1
  160.    the zoomed of Window 1
  161.    the visible of Window 1
  162.  
  163.    the pathname of Document "AE Test"
  164.    the type of Document "AE Test"
  165.    the cell protection of Document "AE Test"
  166.    the object protection of Document "AE Test"
  167.    the number of windows of Document "AE Test"
  168.    the selection of Document "AE Test" as string
  169.    the user selection of Document "AE Test" as string
  170.    the protection of Document "AE Test"
  171.    the name of Document 1
  172.    the modified of Document 1
  173.  
  174.    the name
  175.    the Calculation
  176.    the Iteration
  177.    the selection as string
  178.    the clipboard as string
  179.    the frontmost
  180.    the version as string
  181.  
  182.    the number of series of Chart 1
  183.    the type of Chart 1
  184.    the variant of Chart 1
  185.    the has legend of Chart 1
  186.  
  187.   get (count document 1 each row)
  188.   get (count document 1 each column)
  189.   get index of document "[workbook name]worksheet name"
  190. *)
  191.  
  192. (* end *)
  193.